WPS Office.apk(点击下载) / WeiyunAPI.java


package cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PointerIconCompat;
import android.text.TextUtils;
import cn.wps.moffice.OfficeApp;
import cn.wps.moffice.extlibs.Qing3rdLoginConstants;
import cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI;
import cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun.bean.WYToken;
import cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun.t3rdopen.WeiyunFileModel;
import cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun.t3rdopen.upload.WeiyunUploadTask;
import cn.wps.moffice.main.cloud.storage.model.CSFileData;
import cn.wps.moffice.main.cloud.storage.model.CSFileRecord;
import cn.wps.moffice.main.cloud.storage.model.CSSession;
import cn.wps.moffice_eng.R;
import cn.wps.util.JSONUtil;
import com.baidu.tts.loopj.AsyncHttpResponseHandler;
import com.facebook.ads.AudienceNetworkActivity;
import defpackage.fqx;
import defpackage.frk;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.eclipse.paho.client.mqttv3.internal.ClientDefaults;
import org.scribe.model.OAuthConstants;

public class WeiyunAPI extends AbsCSAPI {
    private static final String TAG = WeiyunAPI.class.getSimpleName();
    private CSFileData gmy;
    private WYToken gnQ;
    private long gnR = 0;
    private frp gnS;
    private fru gnT = new fru(OfficeApp.arx());
    private frq mCoreAPI = new frq();

    public WeiyunAPI(String str) {
        super(str);
        if (this.gmn != null) {
            bCb();
        }
    }

    private static CSFileData a(frn frn) {
        CSFileData cSFileData = new CSFileData();
        String str = frn.url;
        String substring = str.substring(str.lastIndexOf("/") + 1);
        cSFileData.setFileId(substring);
        try {
            cSFileData.setName(URLDecoder.decode(frn.name, AsyncHttpResponseHandler.DEFAULT_CHARSET));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        cSFileData.setFileSize(frn.size);
        cSFileData.setCreateTime(Long.valueOf(frn.ctime));
        cSFileData.setModifyTime(Long.valueOf(frn.mtime));
        cSFileData.setSha1(frn.sha);
        cSFileData.setRevision(frn.sha);
        cSFileData.setPath(substring);
        return cSFileData;
    }

    private static void ab(long j) {
        HashMap hashMap = new HashMap();
        if (0 < j && j < 31457280) {
            hashMap.put("size", "0M_30M");
        } else if (31457280 >= j || j >= 52428800) {
            hashMap.put("size", "50M_MORE");
        } else {
            hashMap.put("size", "30M_50M");
        }
        dwp.l("public_weiyun_file_upload", hashMap);
    }

    private void bCb() {
        this.gnQ = (WYToken) JSONUtil.instance(this.gmn.getToken(), WYToken.class);
    }

    private frp bEW() throws IOException {
        bEX();
        frq frq = this.mCoreAPI;
        WYToken wYToken = this.gnQ;
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("Authorization", "bear " + wYToken.accessToken);
        frp frp = (frp) JSONUtil.instance(frq.goi.a("https://user.weiyun.com/twoa/v1/users/get_info", hashMap), frp.class);
        if (frp == null) {
            throw new ftf(OfficeApp.arx().getString(R.string.documentmanager_tips_network_error));
        } else if (frp.goe > 0) {
            throw new ftf(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW, frp.goe == 1016 ? OfficeApp.arx().getString(R.string.public_weiyun_get_userinfo_error) : frp.god);
        } else if (frp.goe == 0) {
            return frp;
        } else {
            throw new ftf(frp.goe, frp.god);
        }
    }

    private synchronized void bEX() throws IOException {
        if (this.gnQ != null) {
            if (this.gnQ.expiresAt == 0) {
                if (this.gnR == 0 || ((System.currentTimeMillis() - this.gnR) / 1000) + 600 > this.gnQ.expiresIn) {
                    this.gnR = System.currentTimeMillis();
                    WYToken a = this.mCoreAPI.a(this.gnQ);
                    if (a != null) {
                        this.gnQ = a;
                        this.gmn.setToken(JSONUtil.toJSONString(a));
                        this.glC.b(this.gmn);
                    }
                }
            } else if (System.currentTimeMillis() > this.gnQ.expiresAt) {
                this.gnR = System.currentTimeMillis();
                WYToken a2 = this.mCoreAPI.a(this.gnQ);
                if (a2 != null) {
                    a2.calExpiresAt();
                    this.gnQ = a2;
                    this.gmn.setToken(JSONUtil.toJSONString(a2));
                    this.glC.b(this.gmn);
                }
            }
        }
    }

    private List<CSFileData> td(String str) throws fte {
        ArrayList arrayList = new ArrayList();
        boolean z = true;
        int i = 0;
        while (z) {
            ArrayList arrayList2 = new ArrayList();
            try {
                bEX();
                frq frq = this.mCoreAPI;
                WYToken wYToken = this.gnQ;
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("Authorization", "bear " + wYToken.accessToken);
                fro fro = (fro) JSONUtil.instance(frq.goi.a("https://user.weiyun.com/twoa/v1/dirs/" + str + "/list?offset=" + i + "&count=100", hashMap), fro.class);
                if (fro != null && fro.errCode > 0) {
                    throw new IOException(fro.errMsg);
                } else if (fro != null) {
                    if (fro.goc != null) {
                        for (frm frm : fro.goc) {
                            CSFileData cSFileData = new CSFileData();
                            String str2 = frm.url;
                            String substring = str2.substring(str2.lastIndexOf("/") + 1);
                            cSFileData.setFileId(substring);
                            cSFileData.setName(frm.name);
                            cSFileData.setFolder(true);
                            cSFileData.setRefreshTime(Long.valueOf(System.currentTimeMillis()));
                            cSFileData.setCreateTime(0L);
                            cSFileData.setModifyTime(0L);
                            cSFileData.setPath(substring);
                            arrayList2.add(cSFileData);
                        }
                    }
                    if (fro.gob != null) {
                        for (frn frn : fro.gob) {
                            arrayList2.add(a(frn));
                        }
                    }
                    z = !fro.goa;
                    arrayList.addAll(arrayList2);
                    i = arrayList2.size() + i;
                } else {
                    z = false;
                }
            } catch (IOException e) {
                throw new fte(-5, e);
            }
        }
        return arrayList;
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final CSFileData a(CSFileRecord cSFileRecord) throws fte {
        CSFileData sK = sK(cSFileRecord.getFileId());
        CSFileRecord tr = ftb.bGv().tr(cSFileRecord.getFilePath());
        if (tr != null) {
            if (sK == null || !sK.getFileId().equals(tr.getFileId())) {
                throw new fte(-2, "");
            } else if (TextUtils.isEmpty(tr.getFileVer()) || tr.getFileVer().equalsIgnoreCase(sK.getRevision())) {
                return null;
            } else {
                return sK;
            }
        }
        return null;
    }

    @Override // defpackage.fqx
    public final CSFileData a(String str, String str2, ftg ftg) throws fte {
        File file = new File(str2);
        ab(file.length());
        String IR = lwv.IR(str2);
        try {
            bEX();
            this.mCoreAPI.a(this.gnQ, str, IR, file);
            for (CSFileData cSFileData : td(str)) {
                if (!cSFileData.isFolder() && cSFileData.getName().equals(IR)) {
                    return sK(cSFileData.getFileId());
                }
            }
            throw new fte(-2, "文件上传失败:" + IR);
        } catch (IOException e) {
            throw new fte(-5, e);
        }
    }

    @Override // defpackage.fqx
    public final CSFileData a(String str, String str2, String str3, ftg ftg) throws fte {
        File file = new File(str3);
        ab(file.length());
        try {
            bEX();
            this.mCoreAPI.a(this.gnQ, str, file);
            CSFileData sK = sK(str);
            if (sK != null) {
                return sK;
            }
            throw new fte(-2, "文件更新失败。");
        } catch (IOException e) {
            throw new fte(-5, e);
        }
    }

    @Override // defpackage.fqx
    public final List<CSFileData> a(CSFileData cSFileData) throws fte {
        return td(cSFileData.getFileId());
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final void a(final fqx.a aVar) throws fte {
        frk.gnZ = new frk.a() {
            /* class cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun.WeiyunAPI.AnonymousClass1 */

            @Override // defpackage.frk.a
            public final void a(final int i, final String str, final String str2, final String str3) {
                aVar.onLoginBegin();
                new fcv<Void, Void, Boolean>() {
                    /* class cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun.WeiyunAPI.AnonymousClass1.AnonymousClass1 */
                    private Exception dxm;

                    private Boolean aTc() {
                        try {
                            frq frq = WeiyunAPI.this.mCoreAPI;
                            int i = i;
                            String str = str;
                            String str2 = str2;
                            String a = frq.goi.a("https://user.weiyun.com/twoa/v1/auth/authorize?client_id=iciY14xT8lJF0SrlfvIhuTD36VeOkm9R&redirect_uri=wps-office-android://www.wps.cn:12345&response_type=code&state=wps_weiyun_login_state&login_type=" + i + "&appid=" + str + "&openid=" + str2 + "&access_token=" + str3, (HashMap<String, String>) null);
                            WeiyunAPI.this.gnR = System.currentTimeMillis();
                            return Boolean.valueOf(WeiyunAPI.this.q(a));
                        } catch (IOException e) {
                            e.printStackTrace();
                            this.dxm = e;
                        } catch (fte e2) {
                            e2.printStackTrace();
                            this.dxm = e2;
                        }
                        return false;
                    }

                    /* Return type fixed from 'java.lang.Object' to match base method */
                    /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object[]] */
                    /* access modifiers changed from: protected */
                    @Override // defpackage.fcv
                    public final /* synthetic */ Boolean doInBackground(Void[] voidArr) {
                        return aTc();
                    }

                    /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object] */
                    /* access modifiers changed from: protected */
                    @Override // defpackage.fcv
                    public final /* synthetic */ void onPostExecute(Boolean bool) {
                        Boolean bool2 = bool;
                        super.onPostExecute(bool2);
                        if (bool2.booleanValue()) {
                            aVar.bzJ();
                            dwp.km("public_addcloud_weiyun");
                        } else if (this.dxm != null) {
                            aVar.sD(this.dxm.getMessage());
                        } else {
                            aVar.sD(OfficeApp.arx().getString(R.string.documentmanager_toast_login_failed));
                        }
                    }
                }.execute(new Void[0]);
            }

            @Override // defpackage.frk.a
            public final void bEk() {
                aVar.bEk();
            }

            @Override // defpackage.frk.a
            public final void onGoWebViewLogin() {
                aVar.bEl();
            }

            @Override // defpackage.frk.a
            public final void onLoginBegin() {
                aVar.onLoginBegin();
            }

            @Override // defpackage.frk.a
            public final void onLoginFailed(String str) {
                aVar.sD(str);
            }
        };
        Intent intent = new Intent();
        intent.setClassName(OfficeApp.arx(), "cn.wps.moffice.main.cloud.storage.core.service.internal.weiyun.extlibs.WeiyunLoginActivity");
        intent.setFlags(ClientDefaults.MAX_MSG_SIZE);
        OfficeApp.arx().startActivity(intent);
    }

    @Override // defpackage.fqx
    public final boolean a(CSFileData cSFileData, String str, ftg ftg) throws fte {
        try {
            bEX();
            a(str, this.mCoreAPI.a(this.gnQ, cSFileData.getFileId(), cSFileData.getFileSize()), cSFileData.getFileSize(), ftg);
            return true;
        } catch (IOException e) {
            if (fun.b(e)) {
                throw new fte(-6, e);
            }
            throw new fte(-5, e);
        }
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final void bD(String str, String str2) {
        frk.b(2, Qing3rdLoginConstants.WECHAT_APP_ID, str, str2);
    }

    @Override // defpackage.fqx
    public final boolean bE(String str, String str2) throws fte {
        try {
            bEX();
            frq frq = this.mCoreAPI;
            WYToken wYToken = this.gnQ;
            HashMap hashMap = new HashMap();
            hashMap.put("Authorization", "bear " + wYToken.accessToken);
            frr frr = frq.goi;
            HttpPost httpPost = new HttpPost(("https://user.weiyun.com/twoa/v1/files/" + str + "/rename") + "?new_filename=" + URLEncoder.encode(str2, AudienceNetworkActivity.WEBVIEW_ENCODING));
            frr.a((HttpUriRequest) httpPost, (Map<String, String>) hashMap);
            if (!TextUtils.isEmpty(null)) {
                httpPost.setEntity(new StringEntity((String) null, AudienceNetworkActivity.WEBVIEW_ENCODING));
            }
            frn frn = (frn) JSONUtil.instance(EntityUtils.toString(frr.bSN.execute(httpPost).getEntity(), AudienceNetworkActivity.WEBVIEW_ENCODING), frn.class);
            if (frn.errCode <= 0) {
                return frn != null;
            }
            throw new IOException(frn.errMsg);
        } catch (IOException e) {
            throw new fte(-5, e);
        }
    }

    @Override // defpackage.fqx
    public final CSFileData bEA() throws fte {
        if (this.gmy != null) {
            return this.gmy;
        }
        if (this.gnS == null) {
            try {
                this.gnS = bEW();
            } catch (IOException e) {
                int i = -5;
                if (e instanceof ftf) {
                    i = ((ftf) e).mErrorCode;
                }
                throw new fte(i, e.getMessage(), e);
            }
        }
        this.gmy = new CSFileData();
        this.gmy.setName(OfficeApp.arx().getString(R.string.weiyun));
        this.gmy.setModifyTime(Long.valueOf(new Date(0).getTime()));
        this.gmy.setCreateTime(Long.valueOf(new Date(0).getTime()));
        this.gmy.setFileId(this.gnS.goh.substring(this.gnS.goh.lastIndexOf("/") + 1));
        this.gmy.setFolder(true);
        this.gmy.setPath("/");
        this.gmy.setRefreshTime(Long.valueOf(System.currentTimeMillis()));
        return this.gmy;
    }

    @Override // defpackage.fqx
    public final boolean bEx() {
        this.glC.a(this.gmn);
        this.gmn = null;
        this.gnS = null;
        this.gnR = 0;
        return true;
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final String bEy() throws fte {
        return "https://user.weiyun.com/twoa/v1/auth/authorize?client_id=iciY14xT8lJF0SrlfvIhuTD36VeOkm9R&redirect_uri=wps-office-android://www.wps.cn:12345&response_type=code&state=wps_weiyun_login_state";
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final boolean bEz() {
        return true;
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final String getRedirectUrl() {
        return "wps-office-android://www.wps.cn:12345";
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final boolean jv(String str) {
        return frs.bEZ().tf(str) != null;
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final boolean q(String... strArr) throws fte {
        String str;
        Uri parse = Uri.parse(strArr[0]);
        if (parse.getPath() == null) {
            return false;
        }
        try {
            String queryParameter = parse.getQueryParameter(OAuthConstants.CODE);
            if (TextUtils.isEmpty(queryParameter)) {
                return false;
            }
            this.gnQ = this.mCoreAPI.te(queryParameter);
            this.gnQ.calExpiresAt();
            String jSONString = JSONUtil.toJSONString(this.gnQ);
            this.gnS = bEW();
            this.gmn = new CSSession();
            this.gmn.setKey(this.fIH);
            this.gmn.setLoggedTime(System.currentTimeMillis());
            this.gmn.setUserId(new StringBuilder().append(this.gnS.gof).toString());
            this.gmn.setUsername(this.gnS.gog);
            this.gmn.setToken(jSONString);
            this.glC.b(this.gmn);
            frt.bFa().a(new StringBuilder().append(this.gnS.gof).toString(), this.gnQ);
            bCb();
            return true;
        } catch (UnsupportedOperationException e) {
            fqn.c("WeiyunLogin", "handle login result exception...", e);
            throw new fte(-3, OfficeApp.arx().getString(R.string.public_login_error), e);
        } catch (IOException e2) {
            fqn.c("WeiyunLogin", "handle login result exception...", e2);
            int i = -5;
            String string = OfficeApp.arx().getString(R.string.public_login_error);
            if (e2 instanceof ftf) {
                int i2 = ((ftf) e2).mErrorCode;
                String message = e2.getMessage();
                if (i2 <= 0) {
                    i2 = -5;
                }
                if (!TextUtils.isEmpty(message)) {
                    i = i2;
                    str = message;
                } else {
                    i = i2;
                    str = string;
                }
            } else {
                str = string;
            }
            throw new fte(i, str, e2);
        }
    }

    @Override // defpackage.fqx
    public final CSFileData sK(String str) throws fte {
        try {
            bEX();
            frn a = this.mCoreAPI.a(this.gnQ, str);
            if (a != null) {
                return a(a);
            }
            return null;
        } catch (IOException e) {
            throw new fte(-5, e);
        }
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final void sM(String str) {
        this.gnT.start(str);
    }

    @Override // cn.wps.moffice.main.cloud.storage.core.service.internal.AbsCSAPI, defpackage.fqx
    public final void sN(String str) {
        fru fru = this.gnT;
        WeiyunFileModel tf = frs.bEZ().tf(str);
        if (tf != null) {
            String IM = lwl.IM(str);
            if (TextUtils.isEmpty(IM) || !IM.equals(tf.sha)) {
                tf.sha = IM;
                tf.mtime = System.currentTimeMillis();
                tf.size = new File(str).length();
                frs.bEZ().a(tf);
                WeiyunUploadTask weiyunUploadTask = new WeiyunUploadTask();
                weiyunUploadTask.filePath = str;
                weiyunUploadTask.priority = 5;
                weiyunUploadTask.state = 1;
                frv.bFc().d(weiyunUploadTask);
                fru.start(tf.uid);
                fru.goD.get(tf.uid).goJ.offer(weiyunUploadTask);
            }
        }
    }
}